package com.youjiang.meeting.utils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

/**
 * <p>
 * Title: RedPackDistribution
 * </p> 
 * <p>
 * Description:红包分配
 * </p> 
 * <p>
 * Company: YouJiang
 * </p> 
 *
 * @author feng
 * @date 2016年12月3日 下午5:14:07
 * @version 1.0
 */
public class RedPackDistribution {
	public List<Integer> getRedPackDistribution(int count,double amount) throws Exception{
		int money=(int)amount;
		
		List<Integer> splitRedPackets = splitRedPackets(money, count);
	    
		return splitRedPackets;
	}

	private List<Integer> splitRedPackets(double total, int num) {
		// TODO Auto-generated method stub
		List<Integer> money = new ArrayList<>();
		double min=0.10;//红包最小额度
        for (int i=0;i<num-1;i++){                
            double sa_tolat=(total-(num-1-i)*min)/(num-1-i);
            BigDecimal a = new BigDecimal(sa_tolat);
            double x = a.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
            Random rd = new Random();
            double sum_money = (min+rd.nextDouble()) * x;
            BigDecimal b = new BigDecimal(sum_money);
            double y = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
            BigDecimal c = new BigDecimal(Double.toString(total));
            BigDecimal d = new BigDecimal(Double.toString(y));
            total= c.subtract(d).doubleValue();
            System.out.println("第"+(i+1)+"个红包金额=="+y+"余额=="+total);
            
            money.add((int)Math.pow(y*100,1));
        }
        //最后一个红包的金额
        System.out.println("第"+num+"个红包金额=="+total+"余额=="+0.00);
        money.add((int)Math.pow(total*100,1));
        return money;
	}
	
	public static void main(String[] args) throws Exception {
		
//		List<Integer> money = new ArrayList<>();
		System.out.println((int)Math.pow(4.71*100,1));
//		double total=300;//总金额
		RedPackDistribution redpacks = new RedPackDistribution();
		List<Integer> num = redpacks.getRedPackDistribution(50, 1000);
		for(int i:num){
			System.out.println(i);
		}
	}
}
